home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / 011-020 / amok12 / module / sortdemo.mod < prev    next >
Text File  |  1993-11-04  |  2KB  |  84 lines

  1. (*---------------------------------------------------------------------------
  2.     :Program.    SortDemo.mod
  3.     :Author.     Bernd Preusing
  4.     :Address.    Gerhardstr. 16  D-2200 Elmshorn
  5.     :Phone.      04121/22486
  6.     :Shortcut.   [bep]
  7.     :Version.    1.0
  8.     :Date.       27-Oct-88
  9.     :Copyright.  PD
  10.     :Language.   Modula-II
  11.     :Translator. M2Amiga
  12.     :Imports.    ArraySort [bep], Timer2 [bep]
  13.     :UpDate.     
  14.     :Contents.   demostrates Sort and usage of Timer2
  15.     :Remark.     
  16. ---------------------------------------------------------------------------*)
  17. MODULE SortDemo;
  18.  
  19. FROM SYSTEM IMPORT ADR,ADDRESS,BYTE,WORD;
  20. FROM ArraySort IMPORT Sort;
  21. FROM InOut IMPORT Write,WriteLn,WriteString,WriteInt;
  22. FROM RandomNumber IMPORT RND;
  23. FROM Timer2 IMPORT TimeVal,StartTime,StopTime;
  24.  
  25. CONST
  26.   Size = 10000;
  27.  
  28. VAR
  29.   x: ARRAY[1..Size] OF ADDRESS;
  30.   i: LONGCARD;
  31.   t: TimeVal;
  32.  
  33. (* simplest form of prLess: *)
  34. PROCEDURE Less(a,b:ADDRESS):BOOLEAN;
  35. BEGIN
  36.  RETURN a<b
  37. END Less;
  38.  
  39. (* but could be:
  40.    PROCEDURE Less(a,b:ADDRESS): BOOLEAN;
  41.    VAR x1,x2: MyRecPtr;
  42.    BEGIN (* sorts on ascending income and descending age *)
  43.      x1:=CAST(MyRecPtr,a); x2:=CAST(MyRecPtr,b);
  44.      IF x1^.income < x2^.income THEN
  45.        RETURN TRUE
  46.      ELSIF x1^.income > x2^.income THEN
  47.        RETURN FALSE
  48.      ELSE (* same income *)
  49.        RETURN x1^.age > x2^.age
  50.      END;
  51.    END Less;
  52. *)
  53.  
  54. PROCEDURE SortIt();
  55. BEGIN
  56.   StartTime;
  57.   Sort(x,Size,Less);
  58.   StopTime(t);
  59.   WriteString("Ready."); WriteLn;
  60.   WriteInt(t.secs,10); Write('.');
  61.   WriteInt(t.micro DIV 1000,3); WriteString(' seconds.'); WriteLn;
  62.   (* check sort *)
  63.   FOR i:=1 TO Size-1 DO
  64.     IF x[i]>x[i+1] THEN HALT END;
  65.   END;
  66. END SortIt;
  67.  
  68. BEGIN
  69.   WriteString("Calculating 10000 random numbers"); WriteLn;
  70.   FOR i:=1 TO Size DO x[i]:= RND(100000) END;
  71.   WriteString("Sorting 10000 random numbers"); WriteLn;
  72.   SortIt;
  73.  
  74.   WriteString("Sorting 10000 already sorted numbers"); WriteLn;
  75.   SortIt; (* sorted array *)
  76.  
  77.   FOR i:=1 TO Size DO x[i]:=Size-i END;
  78.   WriteString("Sorting 10000 inverse sorted numbers"); WriteLn;
  79.   SortIt;
  80.  
  81.   WriteString('End Program'); WriteLn;
  82.  
  83. END SortDemo.
  84.